package com.hsnn.medstgmini.complain.dao.impl;

import java.util.List;
import java.util.UUID;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.complain.dao.SysComplainAttachmentDao;
import com.hsnn.medstgmini.complain.model.SysComplainAttachment;
/**
 * @category 申投诉附件表DAO接口实现类
 * @author 韩守松
 * @date 2015年10月9日
 */
@Repository
public class SysComplainAttachmentDaoImpl extends BaseHibernateDAO implements SysComplainAttachmentDao {
	/**
	 * @category 保存申投诉相关证明路径
	 * @author 韩守松
	 * @date   2015年10月9日
	 * @param  @param sysComplainAttachment
	 */
	public void save(SysComplainAttachment sysComplainAttachment) {
		sysComplainAttachment.setSysComplainAttrId(UUID.randomUUID().toString().toString());
		getSession().saveOrUpdate(sysComplainAttachment);
		getSession().flush();
		getSession().clear();
		StringBuffer hqlLog = new StringBuffer();
		hqlLog.append("insert into sys_complain_attachment_log  ")
		   .append(" select UUID(),sc.* from sys_complain_attachment sc where sc.sys_complain_attr_id= :complainId ");
		Query queryLog = getSession().createSQLQuery(hqlLog.toString());
		queryLog.setString("complainId", sysComplainAttachment.getSysComplainAttrId());
		queryLog.executeUpdate();
		getSession().flush();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<SysComplainAttachment> getSysComplainById(String complainId) {
		Criteria criteria = getSession().createCriteria(SysComplainAttachment.class);
		criteria.add(Restrictions.eq("sysComplainId", complainId.trim()));
		List<SysComplainAttachment> list = (List<SysComplainAttachment>)criteria.list();
		return list.size()>0?list:null;
	}
}
